package com.yuexun.dhdoctor.module.departmentDocumentNew.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.yuexun.dhdoctor.R;
import com.yuexun.dhdoctor.base.BaseEntity;
import com.yuexun.dhdoctor.config.AppConfig;
import com.yuexun.dhdoctor.module.departmentDocumentNew.Act_Doc_Department_New;
import com.yuexun.dhdoctor.module.departmentDocumentNew.adapter.FilesAdapter;
import com.yuexun.dhdoctor.module.departmentDocumentNew.bean.DocDepartBean;
import com.yuexun.dhdoctor.module.departmentDocumentNew.bean.ShareDocBean;
import com.yuexun.dhdoctor.module.departmentDocumentNew.model.impl.ShareDocModel;
import com.yuexun.dhdoctor.module.departmentDocumentNew.model.impl.UserDepartDocTreeModel;
import com.yuexun.dhdoctor.module.departmentDocumentNew.presenter.impl.ShareDocPresenter;
import com.yuexun.dhdoctor.module.departmentDocumentNew.presenter.impl.UserDepartDocTreePresenter;
import com.yuexun.dhdoctor.module.departmentDocumentNew.view.IShareDocView;
import com.yuexun.dhdoctor.module.departmentDocumentNew.view.IUserDepartDocTreeView;
import com.yuexun.dhdoctor.widget.EmptyLayout;
import com.yuexun.dhdoctor.widget.FixedSwipeListView;
import com.yuexun.dhdoctor.widget.LoadingDialog;


import org.kymjs.kjframe.ui.KJFragment;
import org.kymjs.kjframe.ui.ViewInject;
import org.kymjs.kjframe.utils.PreferenceHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/11/28 0028.
 */

public class Frag_Doc_Department_New extends KJFragment implements IUserDepartDocTreeView, IShareDocView {
    @Bind(R.id.et_doc_department)
    EditText etDocDepartment;
    @Bind(R.id.file_list)
    FixedSwipeListView fileList;
    @Bind(R.id.files_list)
    FixedSwipeListView filesList;
    @Bind(R.id.empty_layout)
    EmptyLayout emptyLayout;

    private LoadingDialog loadingDialog;

    private UserDepartDocTreePresenter docDepartPresenter;
    private ShareDocPresenter shareDocPresenter;

    //当前目录
    private List<DocDepartBean> docDepartBeans = new ArrayList<>();

    FilesAdapter adapter;

    @Override
    protected View inflaterView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
        View view = inflater.inflate(R.layout.frag_doc_department_new, null);
        ButterKnife.bind(this, view);
        initView();
        return view;
    }

    private void initView() {
        loadingDialog = new LoadingDialog(getActivity());
        docDepartPresenter = new UserDepartDocTreePresenter(this, new UserDepartDocTreeModel());
        shareDocPresenter = new ShareDocPresenter(this, new ShareDocModel());
        ApiGetFile();

        fileList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(), Act_Doc_Department_New.class);
                Bundle bundle =new Bundle();
                bundle.putString("info",docDepartBeans.get(position).getChildList());
                bundle.putString("docId",docDepartBeans.get(position).getDepartDocId());
                bundle.putString("type",docDepartBeans.get(position).getDocType()+"");
                bundle.putString("name",docDepartBeans.get(position).getDepartDocName()+"");
                intent .putExtras(bundle);
                startActivity(intent);
            }
        });
    }

    private void ApiGetFile(){
        Map<String, String> params = new HashMap<>();
        params.put("sessionUuid", PreferenceHelper.readString(getActivity(), AppConfig.USER_INFO, AppConfig.SESSION_ID));
        docDepartPresenter.getDepartTree(params);
    }

    //设置文档列表请求
    private void ApiGetDocDatas() {
//        if (nowDepart != null) {
//            Map<String, String> params = new HashMap<>();
//            params.put("sessionUuid", PreferenceHelper.readString(getActivity(), AppConfig.USER_INFO, AppConfig.SESSION_ID));
//            params.put("departDocId", nowDepart.getDepartDocId());
//            params.put("docType", nowDepart.getDocType() + "");
//            params.put("pageIndex", 1 + "");
//            params.put("pageSize", 999999 + "");
////            params.put("inquireKey", key);
//            shareDocPresenter.getShareDocDatas(params);
//        } else {
//            Map<String, String> params = new HashMap<>();
//            params.put("sessionUuid", PreferenceHelper.readString(getActivity(), AppConfig.USER_INFO, AppConfig.SESSION_ID));
//            params.put("departDocId", "a_1");
//            params.put("docType", "1");
//            params.put("pageIndex", 1 + "");
//            params.put("pageSize", 999999 + "");
////            params.put("inquireKey", key);
//            shareDocPresenter.getShareDocDatas(params);
//        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick({R.id.tv_doc_department_search})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_doc_department_search:
                break;
        }
    }

    @Override
    public void showShareDocDatas(BaseEntity<ShareDocBean> getShareDocDatas) {
        if ("200".equals(getShareDocDatas.getStatus())) {
//            adapterDoc.refresh(docDatas);
        } else {
            ViewInject.toast(getShareDocDatas.getMessage());
        }
    }

    @Override
    public void showDepartTree(BaseEntity<DocDepartBean> getDepartTree) {
        if ("200".equals(getDepartTree.getStatus())) {
            ArrayList<DocDepartBean> departDatas;
            departDatas = getDepartTree.getDatas();
            DocDepartBean docDepartBean = new DocDepartBean();
            for (DocDepartBean item : departDatas) {
                if (item.getDepartDocId().equals("a_1")) {
                    docDepartBean = item;
                }
            }
            docDepartBeans = JSON.parseArray(docDepartBean.getChildList(), DocDepartBean.class);

            if (docDepartBeans.size() > 0) {
                adapter = new FilesAdapter(fileList,docDepartBeans,R.layout.item_file);
                fileList.setAdapter(adapter);
                emptyLayout.dismiss();
            } else {
                emptyLayout.setErrorType(EmptyLayout.NODATA);
            }

        }
        else {
            emptyLayout.setErrorType(EmptyLayout.NETWORK_ERROR);
        }
    }
}
